﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Text;
using System.IO;
using System.Web.Mvc;

namespace Gpsys.Web.Common
{
    public static class Helpers
    {
        private static string g_version = DateTime.Now.ToString();

        public static string Version
        {
            get
            {
                return g_version;
            }
        }

        public static string GetActiveMenu(string menu, string current)
        {
            if (menu == current) return "active";

            return "";
        }

        public static string FormatXml(this string unformattedXml)
        {
            if (string.IsNullOrEmpty(unformattedXml))
            {
                return string.Empty;
            }

            try
            {
                //load unformatted xml into a dom
                XmlDocument xd = new XmlDocument();
                xd.LoadXml(unformattedXml);

                //will hold formatted xml
                StringBuilder sb = new StringBuilder();

                //pumps the formatted xml into the StringBuilder above
                StringWriter sw = new StringWriter(sb);

                //does the formatting
                XmlTextWriter xtw = null;

                try
                {
                    //point the xtw at the StringWriter
                    xtw = new XmlTextWriter(sw);

                    //we want the output formatted
                    xtw.Formatting = Formatting.Indented;

                    //get the dom to dump its contents into the xtw 
                    xd.WriteTo(xtw);
                }
                finally
                {
                    //clean up even if error
                    if (xtw != null)
                        xtw.Close();
                }

                //return the formatted xml
                return sb.ToString();
            }
            catch
            {
                return unformattedXml;
            }
        }
    }
}